package jp.ac.u_tokyo.dch.view.outputschema;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.ac.u_tokyo.dch.model.db.DataSchema;
import jp.ac.u_tokyo.dch.model.db.DataSchemaDetail;
import jp.ac.u_tokyo.dch.view.Constants;

public class SchemaView {

	private String sort;

	private String oyaSort;

	private String selectOyaid;

	private DataSchema dataSchema;

	private List<DataSchemaDetail> schemaItemList;

	private List<DataSchemaDetail> customItemList;

	/**
	 * データスキーママップ
	 */
	private Map<String, DataSchemaDetail> dataSchemaDetailMap = null;

	private String editIndex;
	private String type;
	private String optionKbn;
	private String optionResult;

	private String displayname;
	public String getDisplayname() {
		return displayname;
	}


	public void setDisplayname(String displayname) {
		this.displayname = displayname;
	}

	private String customEditIndex;
	private String customItemName;
	private String customInputType;
	private String customOptionKbn;
	private String customOptionResult;

	public SchemaView() {
		dataSchema = new DataSchema();
		schemaItemList = new ArrayList<DataSchemaDetail>();
		customItemList = new ArrayList<DataSchemaDetail>();
	}


	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
		if (Constants.LV2_SORT.equals(sort)) {
			oyaSort = Constants.LV1_SORT;
		} else if (Constants.LV3_SORT.equals(sort)) {
			oyaSort = Constants.LV2_SORT;
		} else if (Constants.LV4_SORT.equals(sort)) {
			oyaSort = Constants.LV3_SORT;
		} else if (Constants.LV5_SORT.equals(sort)) {
			oyaSort = Constants.LV4_SORT;
		} else if (Constants.LV6_SORT.equals(sort)) {
			oyaSort = Constants.LV5_SORT;
		}
	}

	public String getOyaSort() {
		return oyaSort;
	}
	public void setOyaSort(String oyaSort) {
		this.oyaSort = oyaSort;
	}
	public String getSelectOyaid() {
		return selectOyaid;
	}
	public void setSelectOyaid(String selectOyaid) {
		this.selectOyaid = selectOyaid;
	}
	public DataSchema getDataSchema() {
		return dataSchema;
	}
	public void setDataSchema(DataSchema dataSchema) {
		this.dataSchema = dataSchema;
	}

	public List<DataSchemaDetail> getSchemaItemList() {
		return schemaItemList;
	}
	public void setSchemaItemList(List<DataSchemaDetail> schemaItemList) {
		this.schemaItemList = schemaItemList;
	}

	public List<DataSchemaDetail> getCustomItemList() {
		return customItemList;
	}

	public void setCustomItemList(List<DataSchemaDetail> customItemList) {
		this.customItemList = customItemList;
	}

	public String getEditIndex() {
		return editIndex;
	}

	public void setEditIndex(String editIndex) {
		this.editIndex = editIndex;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}


	public String getOptionKbn() {
		return optionKbn;
	}


	public void setOptionKbn(String optionKbn) {
		this.optionKbn = optionKbn;
	}


	public String getOptionResult() {
		return optionResult;
	}

	public void setOptionResult(String optionResult) {
		this.optionResult = optionResult;
	}

	public String getCustomEditIndex() {
		return customEditIndex;
	}

	public void setCustomEditIndex(String customEditIndex) {
		this.customEditIndex = customEditIndex;
	}

	public String getCustomItemName() {
		return customItemName;
	}

	public void setCustomItemName(String customItemName) {
		this.customItemName = customItemName;
	}

	public String getCustomInputType() {
		return customInputType;
	}

	public void setCustomInputType(String customInputType) {
		this.customInputType = customInputType;
	}

	public String getCustomOptionKbn() {
		return customOptionKbn;
	}

	public void setCustomOptionKbn(String customOptionKbn) {
		this.customOptionKbn = customOptionKbn;
	}


	public String getCustomOptionResult() {
		return customOptionResult;
	}

	public void setCustomOptionResult(String customOptionResult) {
		this.customOptionResult = customOptionResult;
	}

	public void editInputFormat() {

		Integer edit = Integer.valueOf(editIndex);

		DataSchemaDetail dataSchemaDetail = schemaItemList.get(edit.intValue());

		dataSchemaDetail.setOptionKbn(optionKbn);
		dataSchemaDetail.setInputType(type);
		dataSchemaDetail.setOptions(optionResult);

	}

	public void addCustomSchema(String fieldKey) {

		DataSchemaDetail dataSchemaDetail = new DataSchemaDetail();

		dataSchemaDetail.setFieldKey(fieldKey);
		dataSchemaDetail.setFieldDisplayname(customItemName);
		dataSchemaDetail.setFieldItemName(customItemName);
		dataSchemaDetail.setInputType(customInputType);
		dataSchemaDetail.setOptionKbn(customOptionKbn);
		dataSchemaDetail.setOptions(customOptionResult);

		customItemList.add(dataSchemaDetail);
	}

	public void editCustomSchema() {

		Integer edit = Integer.valueOf(customEditIndex);

		DataSchemaDetail dataSchemaDetail = customItemList.get(edit.intValue());

		dataSchemaDetail.setFieldDisplayname(customItemName);
		dataSchemaDetail.setFieldItemName(customItemName);
		dataSchemaDetail.setInputType(customInputType);
		dataSchemaDetail.setOptionKbn(customOptionKbn);
		dataSchemaDetail.setOptions(customOptionResult);
	}

	public void deleteCustomSchema() {

		Integer edit = Integer.valueOf(customEditIndex);
		customItemList.remove(edit.intValue());
	}

	public Map<String, DataSchemaDetail> getDataSchemaDetailMap() {

		makeMap();
		return dataSchemaDetailMap;
	}

	private void makeMap() {

		if (dataSchemaDetailMap == null) {
			dataSchemaDetailMap = new HashMap<String, DataSchemaDetail>();
			for (DataSchemaDetail detail : schemaItemList) {
				dataSchemaDetailMap.put(detail.getFieldKey(), detail);
			}
			for (DataSchemaDetail customDetail : customItemList) {
				dataSchemaDetailMap.put(customDetail.getFieldKey(), customDetail);
			}
		}

	}


}
